package com.token.sysflow.hander;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.token.sql.IntegrateSql;
import com.token.sql.PrepareSql;
import com.token.sysorg.bean.FSysPersonnel;
import com.token.sysorg.bean.FSysPost;

import java.sql.ResultSet;
import java.util.Vector;

/**
 * @author chenjianxiong  2020/5/9
 */
public class Person extends FSysPersonnel {
    public Person(IntegrateSql f,int sysId,String userNumber) throws Exception {
        super(f);
        if(userNumber != null) {
        Vector<String> vector = new Vector<>();
            vector.add(String.valueOf(sysId));
            vector.add(userNumber);
            this.initParamToRs("SysId=? and PersonNumber=?", vector);
        }
    }

    public void close(){
        try {
            super.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *
     * @param user
     * @param SysId
     * @return
     * @throws Exception
     */
    public static JSONArray getUserData(IntegrateSql iSql,String user, int SysId) throws Exception{
        JSONArray array = new JSONArray();
        StringBuffer sql = new StringBuffer(100);
        sql.append("select user.PersonNumber as id,user.PersonName as name,post.PostName from ")
                .append(FSysPersonnel.TABLE_NAME)
                .append(" user left join ")
                .append(FSysPost.TABLE_NAME)
                .append(" post on user.PostId=post.PostNumber and user.SysId=post.SysId ");
        sql.append("where user.SysId=? and user.PersonNumber=? ");
        PrepareSql pSql = new PrepareSql(iSql);
        pSql.setPrepare(sql);
        pSql.setPstValue(1,SysId);
        pSql.setPstValue(2,user);
        ResultSet rs = pSql.executeQuery();
        if(rs.next()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("postName",rs.getString("PostName"));
            jsonObject.put("name",rs.getString("name"));
            jsonObject.put("checked",true);
            jsonObject.put("id",rs.getString("id"));
            jsonObject.put("isHas",true);
            array.add(jsonObject);
        }
        return array;
    }
}
